<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-12-17
 * Time: 下午2:27
 */

namespace workin\modules\others\models;


use common\components\tools\Bank;
use common\components\tools\Tools;
use common\models\MultiMediaUploadQuene;
use wodrow\yii\rest\ApiException;
use yii\base\Model;
use yii\db\Exception;

class FormMultiMediaUpload extends Model
{
    public $custom_id;
    public $custom_name;
    public $urls;
    public $idno;

    public function attributeLabels()
    {
        return [
            'custom_id' => "客户ID",
            'custom_name' => "客户名",
            'urls' => "多媒体文件url",
            'idno' => "身份证",
        ];
    }

    public function rules()
    {
        return [
            [['custom_id', 'urls'], 'required'],
            ['custom_id', 'number'],
            ['urls', 'trim'],
            ['urls', 'string'],
            ['custom_name', 'string'],
            ['idno', 'string'],
        ];
    }

    public function addQuene($glue = "\r")
    {
        $quene = MultiMediaUploadQuene::findOne(['custom_id' => $this->custom_id]);
        if (!$quene){
            $quene = new MultiMediaUploadQuene();
            $quene->custom_id = $this->custom_id;
        }else{
            if ($quene->status == 1){
                throw new ApiException(201903061136, "订单正在进行中，请稍后");
            }
        }
        $urls = Tools::str2arr($this->urls, $glue);
        $file_urls = [];
        foreach ($urls as $k => $v){
            $file_urls[] = ['picid' => '8888', 'picurl' => trim($v)];
        }
        $quene->file_urls = json_encode($file_urls);
        $quene->idno = $this->idno;
        $quene->dirname = Bank::DIR_MULTI_MEDIA;
        $quene->status = MultiMediaUploadQuene::STATUS_READY;
        $quene->created_at = time();
        if (!$quene->save()){
            throw new Exception(\common\components\tools\Model::getModelError($quene));
        }
    }
}